package com.dayatang.android.Util;

import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.dayatang.android.Task.BaseTask;
import com.google.android.maps.GeoPoint;

public class MapUtility {
	
	public static JSONObject getLocationInfo(Context context, String address) throws JSONException, InterruptedException, ExecutionException {
		String url = "http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=true";
		JSONObject jsonObject = new JSONObject(new BaseTask(context).execute(url).get());
		return jsonObject;
	}

	public static GeoPoint getGeoPoint(JSONObject jsonObject) {
		Double lon = new Double(0);
		Double lat = new Double(0);
		try {
			lon = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
					.getJSONObject("geometry").getJSONObject("location")
					.getDouble("lng");
			lat = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
					.getJSONObject("geometry").getJSONObject("location")
					.getDouble("lat");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
	}

}
